package com.money.task;

import com.common.constant.RedisKey;
import com.common.domain.po.UserMoney;
import com.money.service.UserMoneyService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CountDownLatch;

/**
 * @author 路亚
 * @version 0.1
 * @className MoneyTask
 * @description
 * @date 2024/12/31 21:30
 * @since jdk11
 */
@Component
@RequiredArgsConstructor
public class MoneyTask {

    private final StringRedisTemplate stringRedisTemplate;

    private final UserMoneyService userMoneyService;

    @Value("${server.port}")
    private Integer port;

    /**
     * 签到，每周一凌晨0点清空redis签到数据
     */
    @XxlJob("signIn")
    public void signIn() {
        Set<String> keys = stringRedisTemplate.keys(RedisKey.SIGN_IN_KEY + "*");
        if (keys != null && keys.size() > 0) {
            for (String key : keys) {
                stringRedisTemplate.delete(key);
            }
        }
    }

    /**
     * 每天定时把金币转换为钱，兑换比例：100金币 = 1分
     * ● 用户可能很多，所以使用线程池
     *   ○ 需要考虑如何设置核心线程数和最大线程数
     * ● 可以借助 xxl-job 使用分片广播模式，执行定时任务
     */
    @XxlJob("convertMoney")
    @Transactional
    public void convertMoney() {
        userMoneyService.turnMoney();
    }
}
